package com.WeChat;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Tab wechat,communication,find,me;
    private ManagerTab managerTab;
    private int currentID;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        wechat=new Tab(new fragment_wechat(getApplicationContext()) ,findViewById(R.id.wechat),findViewById(R.id.txt_wechat) ,findViewById(R.id.icon_wechat),R.color.selected,R.color.common);
        communication=new Tab(new fragment_communication() ,findViewById(R.id.communication),findViewById(R.id.txt_communication) ,findViewById(R.id.icon_communication),R.color.selected,R.color.common);
        find=new Tab(new fragment_find()    ,findViewById(R.id.find)   ,findViewById(R.id.txt_find)    ,findViewById(R.id.icon_find)   ,R.color.selected,R.color.common);
        me=new Tab(new fragment_me()  ,findViewById(R.id.me) ,findViewById(R.id.txt_me)  ,findViewById(R.id.icon_me) ,R.color.selected,R.color.common);


        wechat.linearLayout.setOnClickListener(this);
        communication.linearLayout.setOnClickListener(this);
        find.linearLayout.setOnClickListener(this);
        me.linearLayout.setOnClickListener(this);

        managerTab =new MainActivity.ManagerTab(R.id.content, Arrays.asList(wechat,communication,find,me),getSupportFragmentManager());
        managerTab.initManagerTab();




    }

    @Override
    public void onClick(View view) {
        if(view.getId()==currentID) return;
        switch (view.getId()){
            case R.id.wechat:
                managerTab.switchTab(wechat);
                currentID=R.id.wechat;
                break;
            case R.id.communication:
                managerTab.switchTab(communication);
                currentID=R.id.communication;
                break;
            case R.id.find:
                managerTab.switchTab(find);
                currentID=R.id.find;
                break;
            case R.id.me:
                managerTab.switchTab(me);
                currentID=R.id.me;
                break;
        }
    }

    private static class Tab{
        private Fragment fragment;
        private LinearLayout linearLayout;
        private TextView textView;
        private ImageView imageView;
        private int getFocusedColor;
        private int lostFocusedColor;

        public int getGetFocusedColor() {
            return getFocusedColor;
        }

        public int getLostFocusedColor() {
            return lostFocusedColor;
        }

        public void setGetFocusedColor(int getFocusedColor) {
            this.getFocusedColor = getFocusedColor;
        }

        public void setLostFocusedColor(int lostFocusedColor) {
            this.lostFocusedColor = lostFocusedColor;
        }


        public Tab(Fragment fragment, LinearLayout linearLayout, TextView textView, ImageView imageView) {
            this.fragment = fragment;
            this.linearLayout = linearLayout;
            this.textView = textView;
            this.imageView = imageView;
        }


        public Tab(Fragment fragment, LinearLayout linearLayout, TextView textView, ImageView imageView,int getFocusedColor,int lostFocusedColor) {
            this.fragment = fragment;
            this.linearLayout = linearLayout;
            this.textView = textView;
            this.imageView = imageView;
            this.getFocusedColor=getFocusedColor;
            this.lostFocusedColor=lostFocusedColor;
        }


        private void getFocused(FragmentManager fragmentManager){
            FragmentTransaction transaction= fragmentManager.beginTransaction();
            transaction.show(fragment);
            transaction.commit();
            switchColor(getFocusedColor);
        }


        private void lostFoucs(FragmentManager fragmentManager){
            FragmentTransaction transaction= fragmentManager.beginTransaction();
            transaction.hide(fragment);
            transaction.commit();
            switchColor (lostFocusedColor);
        }


        private void switchColor(int color) {
            textView.setTextColor(color);
            imageView.getDrawable().setTint(color);
        }
    }


    private static class ManagerTab{
        private FragmentManager fragmentManager;
        private int content;
        private List<Tab> tablist;
        private Tab currentTab;


        public ManagerTab(int content,FragmentManager fragmentManager) {
            tablist=new ArrayList<Tab>();
            this.content=content;
            this.fragmentManager=fragmentManager;
        }


        public ManagerTab(int content,List<Tab> tabList,FragmentManager fragmentManager) {
            this.tablist=tabList;
            this.content=content;
            this.fragmentManager=fragmentManager;
        }


        public void add(Tab tab){
            FragmentTransaction transaction=fragmentManager.beginTransaction();
            transaction.add(content,tab.fragment);
            transaction.commit();
            tablist.add(tab);
        }


        private void initManagerTab() {
            FragmentTransaction transaction=fragmentManager.beginTransaction();
            for(Tab tab:tablist){
                transaction.add(content,tab.fragment);
            }
            transaction.commit();
            hideAllTab();
            showTab(tablist.get(0));
        }


        private void hideAllTab() {
            for(Tab tab:tablist){
                tab.lostFoucs(fragmentManager);
            }
        }


        private void showTab(Tab tab) {
            tab.getFocused(fragmentManager);
            currentTab=tab;
        }


        private void switchTab(Tab tab) {
            currentTab.lostFoucs(fragmentManager);
            tab.getFocused(fragmentManager);
            currentTab=tab;
        }
    }



}
